﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcShop.Models.Repositories;
using MvcShop.Models.ViewDataModels;

namespace MvcShop.Models.Services
{
    public interface IOrdersService 
    {
        IEnumerable<OrdersCenter> GetDefaultOrders(string userName);
        IEnumerable<OrdersCenter> GetUserOrders(string userName, int page, int pageSize);
    }

    public class OrdersService : ServiceBase,IOrdersService
    {
        #region ==== IOrdersService 成员 ====
        public IEnumerable<OrdersCenter> GetDefaultOrders(string userName)
        {
            var query = (from od in this.dataContext.Orderses
                         where od.UserName == userName
                         orderby od.OrdersId descending
                         select new OrdersCenter
                         {
                             OrdersId = od.OrdersId,
                             BillNo = od.BillNo,
                             CustomerName = od.CustomerName,
                             Address = od.Address,
                             Ordered = od.Ordered,
                             Phone = od.Phone,
                             Postalcode = od.Postalcode,
                             TotalValue = od.TotalValue,
                             IsInvalid=od.IsInvalid,
                             IsActive = od.IsActive,
                             IsPayment = od.IsPayment,
                             IsSend = od.IsSend,
                             DeliverName = (from dl in this.dataContext.Delivers
                                            where dl.DeliverId == od.DId
                                            select dl.DeliverName).FirstOrDefault()
                         }).Take(5).ToList();

            

            return OrdersHelper(query);   
        }

        public IEnumerable<OrdersCenter> GetUserOrders(string userName, int page, int pageSize)
        {
            var query = (from od in this.dataContext.Orderses
                         where od.UserName == userName
                         orderby od.OrdersId descending
                         select new OrdersCenter
                         {
                             OrdersId = od.OrdersId,
                             BillNo = od.BillNo,
                             CustomerName = od.CustomerName,
                             Address = od.Address,
                             Ordered = od.Ordered,
                             Phone = od.Phone,
                             Postalcode = od.Postalcode,
                             TotalValue = od.TotalValue,
                             IsInvalid = od.IsInvalid,
                             IsActive = od.IsActive,
                             IsPayment = od.IsPayment,
                             IsSend = od.IsSend,
                             DeliverName = (from dl in this.dataContext.Delivers
                                            where dl.DeliverId == od.DId
                                            select dl.DeliverName).FirstOrDefault()
                         }).Skip((page - 1) * pageSize).Take(pageSize).ToList();



            return OrdersHelper(query);
        } 
        #endregion

        public IEnumerable<OrdersCenter> OrdersHelper(IEnumerable<OrdersCenter> ordersCenters)
        { 
            foreach (var oc in ordersCenters)
            {
                oc.OrdersInfos = (from di in this.dataContext.OrdersInfos
                                  join pro in this.dataContext.Products on di.ProductId equals pro.ProductId 
                                  where di.BillNo == oc.BillNo
                                  select new ProductToOrders
                                  {
                                      ProductId = di.ProductId,
                                      Amount = di.Amount,
                                      OPrice = di.OPrice,
                                      ProductName = pro.ProductName,
                                      ImageAddress = (from img in this.dataContext.Product_Images 
                                                      where img.ProductId==di.ProductId 
                                                      select img.ImageAddress).FirstOrDefault()
                                  }).ToList();
            }

            return ordersCenters;
        }
    }
}
